-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove buggy useEffect usage #1316
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All is working! Thanks for the cleanup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you useCallback
for the handler of the <input> onChange
event, and see if adding relevant variables to the dependency arrays in the hooks?
|
||
// Update state when external state is updated | ||
useEffect(() => { | ||
if (initialDate !== date) setDate(initialDate) | ||
if (initialTime !== time) { | ||
setTime(initialTime) | ||
handleTimeChange(initialTime || '') | ||
} | ||
}, [initialTime, initialDate]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eslint is highlighting missing dependencies here: date
, time
, and handleTimeChange
, could you add them to the dependency array as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem is these useEffect hooks are propagating updates from the store into the component, adding those deps would cause a loop. Otherwise I would have just replaced them with callbacks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daniel-heppner-ibigroup Okay, could you add a comment about the undesired side effect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is one in caf809c
) | ||
} | ||
}, [time]) | ||
|
||
useEffect(() => { | ||
if (initialDepartArrive && departArrive !== initialDepartArrive) { | ||
setDepartArrive(initialDepartArrive) | ||
} | ||
}, [initialDepartArrive]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are in fixing dependency arrays, could you adding departArrive
in here too per eslint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above, causes a loop
}, [departArrive, setTime, setDate, homeTimezone]) | ||
} | ||
}, | ||
[syncSortWithDepartArrive, sort, importedUpdateItineraryFilter] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, eslint is suggesting to add dateTime
, handleTimeChange
, homeTimezone
to the dependency array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good note, added them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's give it a shot, thanks for the changes.
Description:
These useEffect usages were causing bugs. Since useEffect calls its callback any time one of the dependencies change, it can be easy to accidentally introduce unexpected bugs. In this case, the query params changing was causing the dateTime effect to be called, which was setting the itinerary sort when external parameters were changing. In this case, we noticed changing the sort order from the itinerary header was causing the effect to run, immediately overriding the setting.
This is actually a good lesson in why useEffect is dangerous!